package com.itheima.reggin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggin.common.CustomException;
import com.itheima.reggin.dto.SetmealDto;
import com.itheima.reggin.entity.Setmeal;
import com.itheima.reggin.entity.SetmealDish;
import com.itheima.reggin.mapper.SetmealMapper;
import com.itheima.reggin.service.SetmealDishService;
import com.itheima.reggin.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
       setmealDishes= setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
       setmealDishService.saveBatch(setmealDishes);
    }
    /**
     * 根据id查询
     * @param setmealDto
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(SetmealDto setmealDto) {
        //查询套餐基本信息
        Setmeal setmeal = this.getById(setmealDto.getId());
        SetmealDto dto = new SetmealDto();
        //数据拷贝
        BeanUtils.copyProperties(setmeal,dto);
        //查询套餐关联信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        dto.setSetmealDishes(setmealDishList);
        return dto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐基本数据
        this.updateById(setmealDto);
        //先删除套餐之前关联的信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //在保存套餐修改的数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes= setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    public void removeByIdsWithDish(List<Long> ids) {
        //查询所删套餐是否是在售状态,是则抛出异常
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(lambdaQueryWrapper);
        if (count>0){
            throw new CustomException("套餐正在售卖,不能删除");
        }
        //删除套餐基本信息
        this.removeByIds(ids);
        //删除套餐关联信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);
    }

    /**
     * 修改状态
     * @param ids
     */
    @Transactional
    public void updateStatus(int status,List<Long> ids) {

        //查询要修改状态的套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmealList = this.list(queryWrapper);
        for (Setmeal setmeal : setmealList) {
            setmeal.setStatus(status);
            this.updateById(setmeal);
        }
    }

}
